package kr.hkit.android_caloriecheck.fragment;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.android_caloriecheck.DBHelper;
import kr.hkit.android_caloriecheck.R;
import kr.hkit.android_caloriecheck.adapter.MyListAdapter;
import kr.hkit.android_caloriecheck.dao.DAO;
import kr.hkit.android_caloriecheck.dto.MyItem;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class ExerciseFragment extends Fragment {
	DBHelper mHelper;
	DAO dao;
	ListView list;
	EditText edit;
	Button search;
	String exerciseName;
	int exerciseKcal;
	
	Calendar calendar;
	int year;
	int month;
	int day;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View root = inflater.inflate(R.layout.exercise_add, container, false);
		
		calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DATE);
		
		mHelper = new DBHelper(getActivity());
		dao = DAO.open(getActivity());
		list = (ListView) root.findViewById(R.id.list_exercise);
		edit = (EditText) root.findViewById(R.id.edit_exercise_add);
		search = (Button) root.findViewById(R.id.btn_exercise_search);
		search.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ArrayList<MyItem> arList;
				String exerciseName = edit.getText().toString().trim();
				
				if(exerciseName.equals("")){
					arList = dao.exerciseSelectAll();
				}else{
					arList = dao.exerciseSelectByName(exerciseName);
				}

				if(arList.size() == 0){
					final LinearLayout linear = (LinearLayout) View.inflate(getActivity(), R.layout.exercise_add02, null);					
					
					new AlertDialog.Builder(getActivity())
					.setTitle("검색결과없음")
					.setView(linear)
					.setPositiveButton("추가", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							EditText exerciseName = (EditText) linear.findViewById(R.id.edit_exercisename);
							EditText exerciseCal = (EditText) linear.findViewById(R.id.edit_esercisecal);
							
							String name = exerciseName.getText().toString().trim();
							String cal = exerciseCal.getText().toString().trim();
							
							try {
								validCheck(name, cal);
								dao.exerciseInsert(name, Integer.parseInt(cal));
								Toast.makeText(getActivity(), name + " " + cal +" 추가", Toast.LENGTH_SHORT).show();
								edit.setText("");
							} catch (Exception e) {
								Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
							}

						}
					})
					.setNegativeButton("취소", null)
					.show();
					
				}else{
					MyListAdapter adapter = new MyListAdapter(getActivity(), arList, R.layout.list, "exercise");
					ListView myList = (ListView) root.findViewById(R.id.list_exercise);
					myList.setAdapter(adapter);
				}
			}
		});
		
		return root;
	}
	
	public void onPause() {
		super.onPause();
		edit.setText("");
	}
	
	public void validCheck(String name, String cal) throws Exception {
		if(name.equals("")){
			throw new Exception("운동명을 입력해주세요.");
		}
		if(cal.equals("")){
			throw new Exception("칼로리를 입력해주세요.");
		}
		
		ArrayList<MyItem> arList = dao.exerciseSelectByName(name);
		if(arList.size() != 0){
			throw new Exception("이미 등록된 정보입니다.");
		}
	}
}
